using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using TST.WebParts.Shared.WebControls;

namespace TST.SharePoint.Shared.WebControls
{

    /// <summary>
    /// Template column to be used in a grid. Renders a dropdown list in the grid.
    /// Used to select a Field object in a grid.
    /// Just allows Url fields to be selected in the dropdown.
    /// </summary>
    public class FieldsDropDownListColumn : ITemplate
    {
        private Fields _dataSource;
        private DatasourceConfiguration _configuration;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="fields">Collection of Fields to be displayed in the dropdown.</param>
        public FieldsDropDownListColumn(Fields fields, DatasourceConfiguration configuration)
        {
            // Create a new field list.
            _dataSource = new Fields();
            _configuration = configuration;

            // Add a new item as the 'empty' value.
            if (!_dataSource.Contains(Guid.Empty.ToString("B")))
            {
                Field emptyField = new Field(Guid.Empty.ToString("B"));
                emptyField.DisplayName = "-- Please select a field --";
                // Mou tekst naar resource file
                _dataSource.Add(emptyField);
            }

            // Add the url fields.
            foreach (Field field in fields)
            {
                if (field.FieldType == FieldType.Url)
                    _dataSource.Add(field);
            }

        }

        public void InstantiateIn(Control container)
        {
            if (container != null)
            {
                SortDropDownList select = new SortDropDownList();
                select.ID = "SelectLinkField";
                select.DataSource = _dataSource;
                select.DataTextField = "DisplayName";
                select.DataValueField = "Id";
                select.DataBinding += new EventHandler(select_DataBinding);
                container.Controls.Add(select);
            }
        }

        private void select_DataBinding(object sender, EventArgs e)
        {
            SortDropDownList dropdownlist = (SortDropDownList)sender;

            // Get the selected value.
            GridViewRow container = (GridViewRow)dropdownlist.NamingContainer;
            //string selected = ((Field)container.DataItem).LinkFieldId;
            string fieldId = ((Field)container.DataItem).Id;
            string selected = null;
            if (_configuration != null && _configuration.Fields.Contains(fieldId))
                selected = _configuration.Fields[fieldId].LinkFieldId;

            // If not item was selected, select the 'empty' item.
            if (string.IsNullOrEmpty(selected))
            {
                selected = Guid.Empty.ToString("B");
            }

            // Select the selected item in the dropdown.
            dropdownlist.SelectedValue = selected;
        }
    }
}
